<?php
// +----------------------------------------------------------------------
// | B5Yii2CMF V3.0 [快捷通用基础管理开发平台]
// +----------------------------------------------------------------------
// | Author=>冰舞 <357145480@qq.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace api\modules\admin\controllers\system;

use api\modules\admin\AuthController;
use common\extend\exception\ErrorCode;
use common\extend\exception\B5HttpException;
use common\helpers\Functions;
use common\helpers\UploadHelper;

class CommonController extends AuthController
{
    /**
     * 公共action操作
     * @return array
     */
    public function actions(): array
    {
        return [
            'upload-img' => [
                'class' => 'common\actions\UploadAction',
                'type' => 'img',
                'success' => ErrorCode::API_SUCCESS,
                'error' => ErrorCode::API_ERROR
            ],
            'upload-video' => [
                'class' => 'common\actions\UploadAction',
                'type' => 'video',
                'success' => ErrorCode::API_SUCCESS,
                'error' => ErrorCode::API_ERROR
            ],
            'upload-file' => [
                'class' => 'common\actions\UploadAction',
                'type' => 'file',
                'success' => ErrorCode::API_SUCCESS,
                'error' => ErrorCode::API_ERROR
            ],
        ];
    }

    /**
     * 上传更新头像
     * @return array
     * @throws B5HttpException
     */
    public function actionUploadAvatar(): array
    {
        $info = $this->getUser();
        $upload = new UploadHelper();
        $result = $upload->setType('img')
            ->setCat('avatar')
            ->setWidth(200)
            ->setHeight(200)
            ->setMaxSize(1024)->run();
        if(!$result['success'])  return $this->error($result['msg']);
        $info->avatar = $result['data']['path'];
        $info->save(false);
        return $this->success('', $result['data']);
    }

    /**
     * 修改密码
     * @return array
     * @throws B5HttpException
     */
    public function actionUpdatePass(): array
    {
        $new_pwd = trim($this->request->post('new_pwd', ''));
        $old_pwd = trim($this->request->post('old_pwd', ''));
        if (!$new_pwd || !$old_pwd) return $this->error('请输入完整信息');
        if (strlen($new_pwd) > 20 || strlen($new_pwd) < 6) return $this->error('密码长度为6-20字符');
        if ($new_pwd == $old_pwd) return $this->error('新密码和原密码不能一致');

        $user = $this->getUser();
        if ($user->password != $user::encryptPassword($old_pwd)) return $this->error('原密码输入错误');
        $user->password = $user::encryptPassword($new_pwd);
        if ($user->save(false)) return $this->success('密码修改成功');
        return $this->error(Functions::getModelError($user));
    }

    /**
     * 获取个人中心信息及更新
     * @return array
     * @throws B5HttpException
     */
    public function actionProfile(): array
    {
        $user = $this->getUser();
        if ($this->request->isPost) {
            $data = [
                'nick_name' => $this->request->post('nick_name', ''),
                'phone' => $this->request->post('phone', ''),
                'email' => $this->request->post('email', ''),
                'sex' => $this->request->post('sex', ''),
            ];
            $user->setScenario('profile');
            if ($user->load($data, '') && $user->save()) {
                return $this->success();
            }
            return $this->error(Functions::getModelError($user));
        } else {
            $userInfo = $user->toArray(['id', 'avatar', 'create_time', 'nick_name', 'phone', 'email', 'sex', 'user_name']);
            return $this->success('', ['user' => $userInfo, 'dept' => $user->deptList ?: [], 'role' => $user->roleList ?: [], 'post' => $user->posList ?: []]);
        }
    }
}
